<?php

namespace App\Http\Controllers;

use App\Models\Message;
use App\Models\Project;
use App\Models\SmsCode;
use App\Models\UserProject;
use App\Models\UserProjectOrder;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use App\Libs\Sms;

class UserOrderController extends Controller
{
    public function index(Request $request)
    {
        $user = $request->user();
        $node = UserProjectOrder::with('volunteer', 'project', 'project.team', 'userProject');
        $perPage = $request->get('perPage', 10);
        $keyword = $request->get('keyword', null);
        $userProjectStatus = $request->get('project_status', null);

        if($user->type == 2)
        {

            $node->whereraw("project_id in (select id from project where team_id={$user->team_id})");
        }elseif($user->type == 3)
        {
            $node->where('org_id', $user->org_id);
        }elseif($user->type == 4)
        {

        }else{
            throw new \Exception('权限不足');
        }

        if($request->get('cash_apply_id'))
        {
            $node = $node->where('cash_apply_id', $request->get('cash_apply_id'));
        }

        if($request->get('cash_max', null)) {
            $node = $node->where('amount', '<=', $request->get('cash_max'));
        }

        if($request->get('cash_min', null)) {
            $node = $node->where('amount', '>=', $request->get('cash_min'));
        }

//        dd(strtotime($request->get('pay_time')));
        if($request->get('pay_time', null)) {
            $node = $node->where('pay_time', '>=', date("Y-m-d", $request->get('pay_time')));
        }

        if ($keyword) {
            $node->where(function($query) use ($keyword){
                $query->orWhereHas('volunteer', function($query) use ($keyword) {
                        $query->where('real_name', 'like', "%{$keyword}%")
                            ->orWhere('phone', 'like', "%{$keyword}%");
                    })
                    ->orWhereHas('project', function($query) use ($keyword) {
                        $query->where('name', 'like', "%{$keyword}%");
                    });
            });
        }

        if ($userProjectStatus) {
            $node->where(function($query) use ($userProjectStatus) {
                $query->orWhereHas('userProject', function ($query) use ($userProjectStatus) {
                    $query->where('verify_status', $userProjectStatus);
                });
            });
        }
//        dd($node->toSql());


        $node->orderBy('id', 'desc');

        $info = $node->paginate($perPage)->toArray();
        $data = $info['data'];
        $projectId = array_map(function($item) {
            return $item['project_id'];
        }, $data);

        $projects = Project::query()->whereIn('id', $projectId)
            ->select('id', 'custom_payment_config')
            ->get()->toArray();
//        dd($projects);
        $goodsKeyLabelMap = [];

        foreach ($projects as $item) {
            if (!$item || !$item['custom_payment_config']) continue;
            foreach ($item['custom_payment_config'] as $cusPayConfig) {
                $goodsKeyLabelMap[$cusPayConfig->key] = $cusPayConfig->label;
            }
        }


//        $userProject = UserProject::whereIn('user_id', $userId)->get()->toArray();
//        dd($data);
        // 获取订单中商品名称，储存在
        foreach ($data as &$item) {
            $goodsInfo = $item['goods_info'];
            $goodsName = [];
            if ($goodsInfo && count($goodsInfo)) {
                foreach ($goodsInfo as $key => $value) {
                    if (isset($goodsKeyLabelMap[$key])) {
                        $goodsName[] = $goodsKeyLabelMap[$key];
                    }
                }
            }
            if (!$item['user_project']) {
                $item['user_project'] = [
                    'verify_status' => 3
                ];
            }

//            foreach ($userProject as $up) {
//                if ($up['user_id'] == $item['user_id']
//                    && $up['project_id'] == $item['project_id']) {
//                    $item['user_project_verify_status'] = $up['verify_status'];
//
//
//                }
//            }
            $item['goods_name'] = $goodsName;
        }
//        dd($data);



        return $this->returnJSON([
            'count' => $info['total'],
            'rows' => $data,
        ]);
    }
}
